Framework
Holds items within a grid layout.
Inventories are an object that contains Item
s in a grid layout. Every Character
will have exactly one inventory attached to
it, which is the only inventory that is allowed to hold bags - any item that has its own inventory (i.e., a suitcase). Inventories
can be owned by a character, or it can be individually interacted with as a standalone object. For example, the container plugin
attaches inventories to props, allowing for items to be stored outside of any character inventories and remain "in the world".
You may be looking for the following common functions:
add Which adds an item to the inventory.
getItems Which gets all of the items inside the inventory.
getID Which gets the inventory's ID.
hasItem Which checks if the inventory has an item.
Functions
inventoryMeta:__eq(other)
Checks if two inventories are equal based on their IDs.
Parameters
-
other
Inventory
The other inventory to compare with.
Returns
-
Boolean
Returns true if the inventories have the same ID, otherwise false.
Example Usage
if inventory1 == inventory2 then
print("Both inventories are the same.")
else
print("Inventories are different.")
end
inventoryMeta:__tostring()
Returns a string representation of the inventory.
Returns
-
String
A string representation of the inventory, including its class name and ID.
Example Usage
print(tostring(inventory))
Output: "Inventory[123]"
inventoryMeta:add(item)
Alias for addItem function.
Parameters
-
item
Item
The item to add to the inventory.
Returns
-
Inventory
Returns the inventory itself.
Example Usage
inventory:add(weapon)
inventoryMeta:addAccessRule(rule, priority)
Adds an access rule to the inventory.
Parameters
-
rule
Function
The access rule function.
-
priority
Integer
optional
The priority of the access rule.
Returns
-
Inventory
Returns the inventory itself.
Example Usage
inventory:addAccessRule(function(inv, action, context)
if action == "remove_item" and context.client:IsAdmin() then
return true
end
end, 10)
inventoryMeta:addDataProxy(key, onChange)
Adds a data proxy to the inventory for a specified key.
Parameters
-
key
any
The key for the data proxy.
-
onChange
Function
The function to call when the data associated with the key changes.
Example Usage
inventory:addDataProxy("health", function(old, new)
print("Health changed from", old, "to", new)
end)
inventoryMeta:addItem(item, noReplicate)
Adds an item to the inventory.
Parameters
-
item
Item
The item to add to the inventory.
-
noReplicate
Boolean
Set to true to prevent OnItemAdded from being called on the added item.
Returns
-
Inventory
Returns the inventory itself.
Example Usage
local weapon = lia.item.new("weapon_rifle")
inventory:addItem(weapon)
inventoryMeta:canAccess(action, context)
Checks if a certain action is permitted for the inventory.
Parameters
-
action
String
The action to check for access.
-
context
Table
Additional context for the access check.
Returns
-
Boolean or nil
Returns true if the action is permitted, false if denied, or nil if not applicable.
-
String[opt]
A reason for the access result.
Example Usage
local canAccess, reason = inventory:canAccess("remove_item", {client = player})
if canAccess then
print("Access granted.")
else
print("Access denied:", reason)
end
inventoryMeta:configure()
Configures the inventory.
Example Usage
function Inventory:configure()
-- Custom configuration
end
inventoryMeta:delete()
Deletes the inventory.
Example Usage
inventory:delete()
inventoryMeta:destroy()
Destroys the inventory and its associated items.
Example Usage
inventory:destroy()
inventoryMeta:extend(className)
Extends the inventory to create a subclass with a specified class name.
Parameters
-
className
String
The name of the subclass.
Returns
-
A
subclass of the Inventory class.
Example Usage
local Inventory = Inventory:extend("GridInv")
inventoryMeta:getData(key, default)
Retrieves data associated with a specified key from the inventory.
Parameters
-
key
String
The key for the data.
-
default
any[opt]
The default value to return if the key does not exist.
Returns
-
any
The value associated with the key, or the default value if the key does not exist.
Example Usage
local health = inventory:getData("health", 100)
print("Health:", health)
inventoryMeta:getFirstItemOfType(itemType)
Retrieves the first item of a specific type from the inventory.
Parameters
-
itemType
String
The type of item to retrieve.
Returns
-
Table or nil
The first item of the specified type, or nil if not found.
Example Usage
local firstHealthPack = inventory:getFirstItemOfType("health_pack")
if firstHealthPack then
print("First Health Pack ID:", firstHealthPack:getID())
end
inventoryMeta:getID()
Retrieves the ID of the inventory.
Returns
-
Integer
The ID of the inventory.
Example Usage
local invID = inventory:getID()
print("Inventory ID:", invID)
inventoryMeta:getItemCount(itemType)
Retrieves the total count of items in the inventory, optionally filtered by item type.
Parameters
-
itemType
String
default: nil
The type of item to count. If nil, counts all items.
Returns
-
The
total count of items in the inventory, optionally filtered by item type.
Example Usage
local totalItems = inventory:getItemCount()
print("Total Items:", totalItems)
local healthPackCount = inventory:getItemCount("health_pack")
print("Health Packs:", healthPackCount)
inventoryMeta:getItems()
Retrieves all items in the inventory.
Returns
-
Table
An array containing all items in the inventory.
Example Usage
local items = inventory:getItems()
for _, item in ipairs(items) do
print("Item ID:", item:getID())
end
inventoryMeta:getItemsByUniqueID(uniqueID, onlyMain)
Retrieves items with a specified unique ID from the inventory.
Parameters
-
uniqueID
String
The unique ID of the items to retrieve.
-
onlyMain
Boolean
Whether to retrieve only main items.
Returns
-
Table
An array containing the items with the specified unique ID.
Example Usage
local weapons = inventory:getItemsByUniqueID("weapon_rifle")
for _, weapon in ipairs(weapons) do
print("Weapon ID:", weapon:getID())
end
inventoryMeta:getItemsOfType(itemType)
Retrieves items of a specific type from the inventory.
Parameters
-
itemType
String
The type of items to retrieve.
Returns
-
Table
An array containing items of the specified type.
Example Usage
local healthPacks = inventory:getItemsOfType("health_pack")
for _, pack in ipairs(healthPacks) do
print("Health Pack ID:", pack:getID())
end
inventoryMeta:getRecipients()
Retrieves the recipients for synchronization.
Returns
-
Table
An array containing the recipients for synchronization.
Example Usage
local recipients = inventory:getRecipients()
for _, client in ipairs(recipients) do
print("Syncing with client:", client:Nick())
end
inventoryMeta:getType()
Retrieves the type of the inventory.
Returns
-
Table
The type of the inventory.
Example Usage
local typeInfo = inventory:getType()
print("Inventory Type:", typeInfo.typeID)
inventoryMeta:hasItem(itemType)
Checks if the inventory contains an item of a specific type.
Parameters
-
itemType
String
The type of item to check for.
Returns
-
Boolean
Returns true if the inventory contains an item of the specified type, otherwise false.
Example Usage
if inventory:hasItem("health_pack") then
print("Inventory contains a health pack.")
else
print("No health packs in inventory.")
end
inventoryMeta:initializeStorage(initialData)
Initializes the storage for the inventory.
Parameters
-
initialData
Table
Initial data for the inventory.
Returns
-
Deferred
A deferred promise.
Example Usage
local promise = inventory:initializeStorage({char = 1, item1 = "value1"})
promise:next(function(invID)
print("Inventory initialized with ID:", invID)
end)
inventoryMeta:instance(initialData)
Instantiates a new inventory instance.
Parameters
-
initialData
Table
Initial data for the inventory instance.
Returns
-
Table
The newly instantiated inventory instance.
Example Usage
local instance = inventory:instance({char = 1, item1 = "value1"})
inventoryMeta:loadItems()
Loads items from the database into the inventory.
Returns
-
Deferred
A deferred promise.
Example Usage
inventory:loadItems():next(function(items)
print("Items loaded:", #items)
end)
inventoryMeta:new()
Creates a new instance of the inventory.
Returns
-
Table
A new instance of the Inventory class.
Example Usage
local newInventory = Inventory:new()
inventoryMeta:onDataChanged(key, oldValue, newValue)
Callback function called when data associated with a key changes.
Parameters
-
key
any
The key whose data has changed.
-
oldValue
any
The old value of the data.
-
newValue
any
The new value of the data.
Example Usage
function Inventory:onDataChanged(key, oldValue, newValue)
print(key, "changed from", oldValue, "to", newValue)
end
inventoryMeta:onInstanced()
Initializes an instance of the inventory.
Example Usage
inventory:onInstanced()
inventoryMeta:onItemsLoaded()
Callback function called when items are loaded into the inventory.
Example Usage
function Inventory:onItemsLoaded(items)
print("Loaded", #items, "items into the inventory.")
end
inventoryMeta:onLoaded()
Callback function called when the inventory is loaded.
Example Usage
function Inventory:onLoaded()
print("Inventory loaded.")
end
inventoryMeta:register(typeID)
Registers the inventory with a specified type ID.
Parameters
-
typeID
String
The type ID to register the inventory with.
Example Usage
inventory:register("grid")
This sets the inventory's type to 'grid'
See Also
inventoryMeta:remove(itemID)
Alias for removeItem function.
Parameters
-
itemID
Integer
The ID of the item to remove.
Returns
-
Deferred
A deferred promise.
Example Usage
inventory:remove(12345):next(function()
print("Item removed.")
end)
inventoryMeta:removeAccessRule(rule)
Removes an access rule from the inventory.
Parameters
-
rule
Function
The access rule function to remove.
Returns
-
Inventory
Returns the inventory itself.
Example Usage
inventory:removeAccessRule(existingRuleFunction)
inventoryMeta:removeItem(itemID, preserveItem)
Removes an item from the inventory.
Parameters
-
itemID
Integer
The ID of the item to remove.
-
preserveItem
Boolean
Whether to preserve the item's data in the database.
Returns
-
Deferred
A deferred promise.
Example Usage
inventory:removeItem(12345, true):next(function()
print("Item removed while preserving data.")
end)
inventoryMeta:setData(key, value)
Sets data associated with a key in the inventory.
Parameters
-
key
any
The key to associate the data with.
-
value
any
The value to set for the key.
Returns
-
Inventory
Returns the inventory itself.
Example Usage
inventory:setData("owner", player)
inventoryMeta:show(parent)
Displays the inventory UI to the specified parent element.
Parameters
-
parent
any
optional
The parent element to which the inventory UI will be displayed.
Returns
-
any
The result of the lia.inventory.show function.
Example Usage
inventory:show(panel)
inventoryMeta:sync(recipients)
Synchronizes the inventory with clients.
Parameters
-
recipients
Table
The recipients to synchronize with.
Example Usage
inventory:sync()
inventoryMeta:syncData(key, recipients)
Synchronizes data changes with clients.
Parameters
-
key
any
The key whose data has changed.
-
recipients
Table
The recipients to synchronize with.
Example Usage
inventory:syncData("health", {client = player})
inventoryMeta:syncItemAdded(item)
Synchronizes the addition of an item with clients.
Parameters
-
item
Item
The item being added.
Example Usage
inventory:syncItemAdded(weapon)